<template>
  <div class="all-goods">
    <div class="banxin">
      <Breadcrumb :arr="BreadcrumbList"></Breadcrumb>
      <Banner :src="ad" class="banner"></Banner>
      <div class="jifen flex">
        <div class="left">积分：</div>
        <ul class="flex">
          <li v-for="item in priceGroup" :key="item.id">
            <span
              :class="{ active: priceId === item.id }"
              @click="getPriceDate(item)"
              >{{ item.text }}</span
            >
          </li>
        </ul>
      </div>
      <div class="cate flex">
        <div class="left">分类：</div>
        <ul class="flex">
          <li v-for="item in cateGroup" :key="item.id">
            <span
              :class="{ active: cateId === item.id }"
              @click="getCateDate(item)"
              >{{ item.text }}</span
            >
          </li>
        </ul>
      </div>
      <!-- 商品列表 -->
      <GoodList :arr="goodsList"></GoodList>
      <p>
        <i class="iconfont icon-loading" v-show="isLoading"></i>
      </p>
      <p style="text-align: center">没有更多数据了</p>
    </div>
  </div>
</template>
 
<script>
import Banner from "../components/home/Banner.vue";
import ad from "../assets/images/home/ad.png";
import GoodList from "../components/home/GoodList.vue";
import { getGoodsListApi } from "../http/api";
import Breadcrumb from "../components/common/Breadcrumb.vue";
import { toast } from "../utils/toast";
export default {
  data() {
    return {
      ad,
      priceId: 1,
      cateId: 10,
      currentMin: 0,
      currentMax: 0,
      currentCate: 0,
      BreadcrumbList: [
        { path: "/home", text: "主页" },
        { path: "/allgoods", text: "全部商品", isLast: true, isCurrent: true },
      ],
      priceGroup: [
        { id: 1, text: "全部", min: 0, max: 0 },
        { id: 2, text: "我能兑换的", min: 0, max: 0 },
        { id: 3, text: "0-500分", min: 0, max: 500 },
        { id: 4, text: "500-1500分", min: 500, max: 1500 },
        { id: 5, text: "1500以上", min: 1500, max: 100000 },
      ],
      cateGroup: [
        { id: 1, text: "全部" },
        { id: 2, text: "实物礼品" },
        { id: 3, text: "虚拟礼品" },
      ],
      goodsList: [],
      // 用来储存分好得数据得数组
      pagesList: [],
      CurrentPages: 1,
      isLoading: false,
      isTimeout: true,
    };
  },
  components: {
    Banner,
    GoodList,
    Breadcrumb,
  },
  created() {
    this.getgoodsData();
  },
  methods: {
    getPriceDate(item) {
      if (item.id == 2) {
        if (this.$store.state.userInfo.coin !== undefined) {
          this.priceGroup[1].max = this.$store.state.userInfo.coin;
        } else {
          toast("请先登录", "warning");
          this.$store.commit("modifyShowLogin", true);
        }
      }
      this.priceId = item.id;
      this.currentMin = item.min;
      this.currentMax = item.max;
      this.getgoodsData();
    },
    getCateDate(item) {
      this.cateId = item.id;
      this.currentCate = item.type;
      this.getgoodsData();
    },
    async getgoodsData() {
      // {data} 是解构
      // const { data } = await getGoodsListApi();
      // // console.log(res);
      // this.goodsList = data.map((e) => {
      //   e.coverImg = `http://sc.wolfcode.cn` + e.coverImg;
      //   return e;
      // });
      const params = {
        type: this.currentCate,
        min: this.currentMin,
        max: this.currentMax,
        did: this.$route.query.did || 0,
        keyword: this.$route.query.keyword || "",
      };
      // console.log(params);
      const { data } = await getGoodsListApi(params);
      // this.goodsList = data.map((e) => {
      //   e.coverImg = `http://sc.wolfcode.cn` + e.coverImg;
      //   return e;
      // });
      // this.goodsList = data;
      const size = 8;
      const pages = Math.ceil(data.length / size);
      for (let i = 1; i <= pages; i++) {
        this.pagesList[i] = data.slice((i - 1) * size, i * size);
      }
      // 拿到数组来看看 有四个数组
      console.log(this.pagesList);
      // 如果直接丢 只是丢数组 需要用数据得话 得展开
      this.goodsList.push(...this.pagesList[this.CurrentPages]);
    },
  },
  watch: {
    // 监听内部变化
    // 路由传递数据在当前页面刷新不会触发请求的问题
    "$route.query.keyword": {
      handler() {
        // 重新获取数据
        this.getgoodsData();
      },
      deep: true,
    },
  },
  mounted() {
    window.onscroll = () => {
      const out = document.documentElement.scrollTop || document.body.scrollTop;
      const bh =
        document.documentElement.clientHeight || document.body.clientHeight;
      const ph =
        document.documentElement.offsetHeight || document.body.offsetHeight;
      if (out + bh >= ph - 200) {
        if (this.isTimeout) {
          this.isTimeout = false;
          setTimeout(() => {
            this.isTimeout = true;
          }, 2500);
          this.CurrentPages++;
          if (this.CurrentPages >= this.pagesList.length) return;
          this.isLoading = true;
          // 如果已经加载全部数据
          setTimeout(() => {
            if (this.CurrentPages < this.pagesList.length) {
              this.goodsList.push(...this.pagesList[this.CurrentPages]);
              this.isLoading = false;
            }
          }, 300);
        }
      }
    };
  },
  beforeDestroy() {
    window.onscroll = null;
  },
};
</script>
 
<style lang = "less" scoped>
.all-goods {
  background-color: #f5f5f5;
  .banxin {
    .flex {
      padding: 10px 0;
    }
    .left {
      font-size: 16px;
      font-family: SourceHanSansSC;
      font-weight: bold;
      color: #333333;
      line-height: 36px;
    }
    li {
      color: #666;
      padding: 0 10px;
      span {
        cursor: pointer;
        &.active {
          color: #00329b;
        }
      }
    }
  }
}
.iconfont.icon-loading {
  display: block;
  margin: auto;
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  animation: loading 1s linear infinite;
}
@keyframes loading {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>